//
//  STOrderViewController.m
//  赛拓
//
//  Created by 王磊 on 7/28/16.
//  Copyright © 2016 立金所. All rights reserved.
//

#import "STOrderViewController.h"
#import "STTitleView.h"
#import "STSellersViewController.h"
#import "STBuyersViewController.h"
@interface STOrderViewController ()<STBaseViewDelegate>

@property (nonatomic ,strong) NSArray *viewControllers;

@property (nonatomic ,strong) UIPageViewController *page;

@property (nonatomic ,assign) NSInteger selectedIndex;
@end

@implementation STOrderViewController

- (void)initSubviews {
    [super initSubviews];
    
    [self initTitleView];
    
    [self initPageVc];
}
- (void)initTitleView {
    STTitleView *titleView = [STTitleView instance];
    
    self.navigationItem.titleView = titleView;
    
    titleView.frame = CGRectMake(0, 0, 200 * ST_RATE, 44);
    
    titleView.mDelegate = self;
    
    titleView.titles = @[@"买家",@"卖家"];
    
}
- (void)loadData {
    
    
}

- (void)initPageVc {
    
    self.selectedIndex = 0;
    
    STSellersViewController *seller = [STSellersViewController instance];
    @WeakObj(self)
    seller.block =  ^(id itemClickType ,id mDate ,id indexPath) {
        STItemClickType itemType = [itemClickType integerValue];
        
        [selfWeak onItemClick:itemType withData:mDate forIndexPath:indexPath];
        
    };
    
    STBuyersViewController *buyers = [STBuyersViewController instance];
    
    buyers.block =  ^(id itemClickType ,id mDate ,id indexPath) {
        STItemClickType itemType = [itemClickType integerValue];
        
        [selfWeak onItemClick:itemType withData:mDate forIndexPath:indexPath];
        
    };
    
    self.viewControllers = @[buyers ,seller];
    
    UIPageViewController *pageVC = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:@{UIPageViewControllerOptionSpineLocationKey: @(UIPageViewControllerSpineLocationMin)}];
    
    self.page = pageVC;
    
    [pageVC setViewControllers:@[self.viewControllers[self.selectedIndex]] direction:UIPageViewControllerNavigationDirectionForward animated:true completion:nil];
    
    pageVC.view.frame = CGRectMake(0, 0, ST_SCREEN_WIDTH, CGRectGetHeight(self.view.bounds));
    
    [self.view addSubview:pageVC.view];
    
}
- (void)onItemClick:(STItemClickType)itemType withData:(id)data forIndexPath:(NSIndexPath *)indexPath {
    switch (itemType) {
        case STItemClickTypeTitle:
        {
            
            UIPageViewControllerNavigationDirection direction = indexPath.row > self.selectedIndex ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
            
            UIViewController *vc = self.viewControllers[indexPath.row];
            
            [self.page setViewControllers:@[vc] direction:direction animated:true completion:nil];
            
            self.selectedIndex = indexPath.row;
            
            
        }
            break;
            
        default:
            break;
    }
    
    
}
@end
